package com.ruoyi.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BaseCatalog;
import com.ruoyi.base.service.IBaseCatalogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 数据目录Controller
 * 
 * @author yepanpan
 * @date 2025-02-12
 */
@RestController
@RequestMapping("/base/catalog")
public class BaseCatalogController extends BaseController
{
    @Autowired
    private IBaseCatalogService baseCatalogService;

    /**
     * 查询数据目录列表
     */
    @PreAuthorize("@ss.hasPermi('base:catalog:list')")
    @GetMapping("/list")
    public AjaxResult list(BaseCatalog baseCatalog)
    {
        List<BaseCatalog> list = baseCatalogService.selectBaseCatalogList(baseCatalog);
        return success(list);
    }

    /**
     * 导出数据目录列表
     */
    @PreAuthorize("@ss.hasPermi('base:catalog:export')")
    @Log(title = "数据目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseCatalog baseCatalog)
    {
        List<BaseCatalog> list = baseCatalogService.selectBaseCatalogList(baseCatalog);
        ExcelUtil<BaseCatalog> util = new ExcelUtil<BaseCatalog>(BaseCatalog.class);
        util.exportExcel(response, list, "数据目录数据");
    }

    @Log(title = "数据目录管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:catalog:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseCatalog> util = new ExcelUtil<BaseCatalog>(BaseCatalog.class);
        List<BaseCatalog> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = baseCatalogService.importData(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BaseCatalog> util = new ExcelUtil<BaseCatalog>(BaseCatalog.class);
        util.importTemplateExcel(response, "数据目录数据");
    }

    /**
     * 获取数据目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:catalog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseCatalogService.selectBaseCatalogById(id));
    }

    /**
     * 新增数据目录
     */
    @PreAuthorize("@ss.hasPermi('base:catalog:add')")
    @Log(title = "数据目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BaseCatalog baseCatalog)
    {
        return toAjax(baseCatalogService.insertBaseCatalog(baseCatalog));
    }

    /**
     * 修改数据目录
     */
    @PreAuthorize("@ss.hasPermi('base:catalog:edit')")
    @Log(title = "数据目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BaseCatalog baseCatalog)
    {
        return toAjax(baseCatalogService.updateBaseCatalog(baseCatalog));
    }

    /**
     * 删除数据目录
     */
    @PreAuthorize("@ss.hasPermi('base:catalog:remove')")
    @Log(title = "数据目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseCatalogService.deleteBaseCatalogByIds(ids));
    }
}
